Remove remaining callers and definition of rtrim. (#1001)
authorGPSBabel <12013583+GPSBabelDeveloper@users.noreply.github.com>
Sat, 18 Feb 2023 17:39:15 +0000 (11:39 -0600)
committerGitHub <noreply@github.com>
Sat, 18 Feb 2023 17:39:15 +0000 (11:39 -0600)
Remove remaining callers and definition of rtrim.

* Minor CV-qualifier battle in jeeps for const correctness.
* Adds a (hacky) bash script to compile mkshort as a standalone binary for
development and testing for upcoming overhaul.

---------

Co-authored-by: Robert Lipe <robertlipe@gmail.com>
defs.h
garmin_txt.cc
jeeps/gpsmath.cc
jeeps/gpsmath.h
mkshort.cc
tools/mk_short_test [new file with mode: 0755]
unicsv.cc
unicsv.h
util.cc

diff --git a/defs.h b/defs.h
index 495453cad8803047cc1893b98ccb040fbc9bc3ec..3c752e7229dae673bc8f047140cd9339466a7cd6 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -1018,7 +1018,6 @@ inline int case_ignore_strncmp(const QString& s1, const QString& s2, int n)
 
 int str_match(const char* str, const char* match);
 
-void rtrim(char* s);
 int xasprintf(char** strp, const char* fmt, ...) PRINTFLIKE(2, 3);
 int xasprintf(QString* strp, const char* fmt, ...) PRINTFLIKE(2, 3);
 int xasprintf(QScopedPointer<char, QScopedPointerPodDeleter>& strp, const char* fmt, ...) PRINTFLIKE(2, 3);
index 2c8151770c1af22a9f8e4ceccbde0c71ad86eaf6..ed5b19c3e36cf2e0c5b94ace5181fa64ee220776 100644 (file)
@@ -116,7 +116,7 @@ inline gt_display_modes_e operator++(gt_display_modes_e& s, int) // postfix
 
 #define MAX_HEADER_FIELDS 36
 
-static char* header_lines[unknown_header + 1][MAX_HEADER_FIELDS];
+static QString header_lines[unknown_header + 1][MAX_HEADER_FIELDS];
 static int header_fields[unknown_header + 1][MAX_HEADER_FIELDS];
 static int header_ct[unknown_header + 1];
 
@@ -869,11 +869,7 @@ static void
 free_header(const header_type ht)
 {
   for (int i = 0; i < MAX_HEADER_FIELDS; i++) {
-    char* c = header_lines[ht][i];
-    if (c != nullptr) {
-      xfree(c);
-      header_lines[ht][i] = nullptr;
-    }
+    header_lines[ht][i].clear();
   }
   header_ct[ht] = 0;
   memset(header_fields[ht], 0, sizeof(header_fields[ht]));
@@ -995,7 +991,8 @@ parse_header(const QStringList& lineparts)
 
   for (const auto& str : lineparts) {
     column++;
-    header_lines[unknown_header][column] = strupper(xstrdup(str));
+    header_lines[unknown_header][column] = str;
+    header_lines[unknown_header][column] = header_lines[unknown_header][column].toUpper();
     header_ct[unknown_header]++;
     if (header_ct[unknown_header] >= MAX_HEADER_FIELDS) {
       break;
@@ -1044,13 +1041,13 @@ bind_fields(const header_type ht)
   }
 
   for (i = 0; i < header_ct[unknown_header]; i++) {
-    char* name = header_lines[ht][i] = header_lines[unknown_header][i];
-    header_lines[unknown_header][i] = nullptr;
+    auto name = header_lines[ht][i] = header_lines[unknown_header][i];
+    header_lines[unknown_header][i].clear();
 
     c = fields;
     int field_no = 1;
     while (*c) {
-      if (strcmp(c, name) == 0) {
+      if (name.compare(c) == 0) {
         header_fields[ht][i] = field_no;
 #if 0
         printf("Binding field \"%s\" to internal number %d (%d,%d)\n", name, field_no, ht, i);
index ce6aba487307fe94a7cb381807226be516c5e5d3..b489c5bf6057e553775c86622e0897ab818219af 100644 (file)
@@ -1380,7 +1380,7 @@ int32 GPS_Math_EN_To_UKOSNG_Map(double E, double N, double* mE,
 ** Convert Ordnance Survey map eastings and northings plus
 ** two letter code to Airy 1830 eastings and northings
 **
-** @param [w] map [char *] map code
+** @param [w] map [const char *] map code
 ** @param [r] mapE [double] easting (metres)
 ** @param [r] mapN [double] northing (metres)
 ** @param [w] E [double *] full Airy easting (metres)
@@ -1389,8 +1389,8 @@ int32 GPS_Math_EN_To_UKOSNG_Map(double E, double N, double* mE,
 **
 ** @return [int32] success
 ************************************************************************/
-int32 GPS_Math_UKOSNG_Map_To_EN(char* map, double mapE, double mapN, double* E,
-                                double* N)
+int32 GPS_Math_UKOSNG_Map_To_EN(const char* map, double mapE, double mapN,
+                                double* E, double* N)
 {
   int32  t;
   int32  idx;
@@ -1883,7 +1883,7 @@ int32 GPS_Math_WGS84_To_UKOSMap_M(double lat, double lon, double* mE,
 ** Transform UK Ordnance survey map position to WGS84 lat/lon
 ** Uses Molodensky transformation
 **
-** @param [r] map  [char *] map two letter code
+** @param [r] map  [const char *] map two letter code
 ** @param [r] mE   [double] map easting (metres)
 ** @param [r] mN   [double] map northing (metres)
 ** @param [w] lat  [double *] WGS84 latitude (deg)
@@ -1891,7 +1891,7 @@ int32 GPS_Math_WGS84_To_UKOSMap_M(double lat, double lon, double* mE,
 **
 ** @return [int32] success
 ************************************************************************/
-int32 GPS_Math_UKOSMap_To_WGS84_M(char* map, double mE, double mN,
+int32 GPS_Math_UKOSMap_To_WGS84_M(const char* map, double mE, double mN,
                                   double* lat, double* lon)
 {
   double E;
@@ -1962,7 +1962,7 @@ int32 GPS_Math_WGS84_To_UKOSMap_C(double lat, double lon, double* mE,
 **
 ** @return [int32] success
 ************************************************************************/
-int32 GPS_Math_UKOSMap_To_WGS84_C(char* map, double mE, double mN,
+int32 GPS_Math_UKOSMap_To_WGS84_C(const char* map, double mE, double mN,
                                   double* lat, double* lon)
 {
   double E;
index f3915ad57571d1b9e3bb21456ebd2ec500679c83..36d78436d213ce83c2c224eaef487dcb001d07b5 100644 (file)
@@ -32,7 +32,7 @@
                                         double* N);
   int32  GPS_Math_EN_To_UKOSNG_Map(double E, double N, double* mE,
                                    double* mN, char* map);
-  int32  GPS_Math_UKOSNG_Map_To_EN(char* map, double mapE, double mapN,
+  int32  GPS_Math_UKOSNG_Map_To_EN(const char* map, double mapE, double mapN,
                                    double* E, double* N);
 
   void GPS_Math_LatLonH_To_XYZ(double phi, double lambda, double H,
 
   int32 GPS_Math_WGS84_To_UKOSMap_M(double lat, double lon, double* mE,
                                     double* mN, char* map);
-  int32 GPS_Math_UKOSMap_To_WGS84_M(char* map, double mE, double mN,
+  int32 GPS_Math_UKOSMap_To_WGS84_M(const char* map, double mE, double mN,
                                     double* lat, double* lon);
   int32 GPS_Math_WGS84_To_UKOSMap_C(double lat, double lon, double* mE,
                                     double* mN, char* map);
-  int32 GPS_Math_UKOSMap_To_WGS84_C(char* map, double mE, double mN,
+  int32 GPS_Math_UKOSMap_To_WGS84_C(const char* map, double mE, double mN,
                                     double* lat, double* lon);
 
 
index 1d50860d887f4ef0495799eb0a930e5110f1f79f..c343b2519e51435cf33b35aedbd47833b852af1e 100644 (file)
@@ -473,6 +473,15 @@ mkshort(short_handle h, const char* istring, bool is_utf8)
     }
   }
 
+  /* Eliminate trailing whitespace in all cases. This is done late because
+     earlier operations may have vacated characters after the space. */
+
+  char *end = ostring + strlen(ostring) - 1;
+  while (end > ostring && isspace(*end)) {
+    *end-- = 0;
+  }
+
+
   /*
    * Toss vowels to approach target length, but don't toss them
    * if we don't have to.  We always keep the leading two letters
@@ -535,14 +544,20 @@ mkshort(short_handle h, const char* istring, bool is_utf8)
     }
     memmove(dp, np, nlen);
     dp[nlen] = 0;
-    rtrim(ostring);
+    // Essentially ostring.rtrim() from here down.
+    if (ostring && ostring[0] != 0) { // Empty output string? Bail.
+      char *end = ostring + strlen(ostring) - 1;
+      while (end > ostring && isspace(*end)) {
+        *end-- = 0;
+      }
+    }
   }
 
   /*
    * If, after all that, we have an empty string, punt and
    * let the must_uniq code handle it.
    */
-  if (ostring[0] == '\0') {
+  if (ostring && ostring[0] == '\0') {
     xfree(ostring);
     ostring = xstrdup(hdl->defname);
   }
@@ -595,8 +610,8 @@ mkshort_from_wpt(short_handle h, const Waypoint* wpt)
 }
 
 
-#if 0
-char* foo[] =  {
+#if TEST_MKSHORT
+const char* foo[] =  {
   "VwthPst# 3700.706N 08627.588W 0000000m View the Past #2              ",
   "PilotRoc 3655.270N 08717.173W 0000000m Pilot Rock by CacheAdvance    ",
   "MrCycsNg 3652.407N 08728.890W 0000000m Mr. Cayces Neighborhood by Ca",
@@ -738,36 +753,47 @@ char* foo[] =  {
   "GimmShlt 3430.087N 08536.834W 0000000m Gimme Shelter by Big Rock & Po",
   "GnomeTrs 3433.081N 08535.849W 0000000m Gnome Treasure by Big Rock    ",
   "FlyingTr 3608.594N 08648.179W 0000000m  Flying Torso  by Campaholics ",
-  "CultivtC 3608.582N 08648.064W 0000000m  Cultivate a Cure  by Campahol"
+  "CultivtC 3608.582N 08648.064W 0000000m  Cultivate a Cure  by Campahol",
+  __nullptr
 }
 ;
 
-main()
+int main()
 {
-  char** foop = foo;
   int r;
 
-  printf("%s\n", mkshort("The Troll"));
-  printf("%s\n", mkshort("EFI"));
-  printf("%s\n", mkshort("the Troll"));
-  printf("%s\n", mkshort("the Trolley"));
-  printf("%s\n", mkshort("The Troll Lives Under The Bridge"));
-  printf("%s\n", mkshort("The \"Troll\" Lives Under $$ The Bridge!"));
-  printf("%s\n", mkshort("The Trolley Goes Round"));
-  printf("%s\n", mkshort("Cache In / Trash Out #1"));
-  printf("%s\n", mkshort("Cache In / Trash Out #2"));
-  printf("%s\n", mkshort("Cache In / Trash Out #137"));
-
-  while (0 && *foop) {
-    printf("%s %s\n", mkshort(*foop+39), *foop+39);
+  auto h = mkshort_new_handle();
+//  setshort_whitespace_ok(h, false);
+
+  printf("%s\n", mkshort(h, "The Troll", false));
+  printf("%s\n", mkshort(h, "EFI", false));
+  printf("%s\n", mkshort(h, "the Troll", false));
+  printf("%s\n", mkshort(h, "the Trolley", false));
+  printf("%s\n", mkshort(h, "The Troll Lives Under The Bridge", false));
+  printf("%s\n", mkshort(h, "The \"Troll\" Lives Under $$ The Bridge!", false));
+  printf("%s\n", mkshort(h, "The Trolley Goes Round", false));
+  printf("%s\n", mkshort(h, "Cache In / Trash Out #1", false));
+  printf("%s\n", mkshort(h, "Cache In / Trash Out #2", false));
+  printf("%s\n", mkshort(h, "Cache In / Trash Out #137", false));
+  printf("|%s|\n", mkshort(h, "Cache .", false));
+  printf("|%s|\n", mkshort(h, "Cache    .", false));
+  printf("|%s|\n", mkshort(h, "Cache         .", false));
+  printf("|%s|\n", mkshort(h, "   Cache   .", false));
+
+#if 0
+  const char** foop = foo;
+  while (*foop) {
+    printf("%s | %s\n", mkshort(h, *foop+39, false), *foop+39);
     foop++;
   }
+#endif
 
-  printf("%s\n", delete_last_vowel(0, "the quick brown foo", &r));
-  printf("%s\n", delete_last_vowel(0, "the quick brown fox", &r));
-  printf("%s\n", delete_last_vowel(0, "xxx", &r));
-  printf("%s\n", delete_last_vowel(0, "ixxx", &r));
-  printf("%s\n", delete_last_vowel(0, "aexxx", &r));
+  printf("%s\n", delete_last_vowel(0, xstrdup("the quick brown foo"), &r));
+  printf("%s\n", delete_last_vowel(0, xstrdup("the quick brown fox"), &r));
+  printf("%s\n", delete_last_vowel(0, xstrdup("xxx"), &r));
+  printf("%s\n", delete_last_vowel(0, xstrdup("ixxx"), &r));
+  printf("%s\n", delete_last_vowel(0, xstrdup("aexxx"), &r));
 
+  return 0;
 }
 #endif
diff --git a/tools/mk_short_test b/tools/mk_short_test
new file mode 100755 (executable)
index 0000000..6f7281c
--- /dev/null
@@ -0,0 +1,38 @@
+#
+# Definitely a kludge, but build mkshort in a way that the output can be run
+# and visually inspected. Unlikely to work for anyone but robert.
+#
+
+c++ \
+  -DTEST_MKSHORT \
+  -DCSVFMTS_ENABLED \
+  -DFILTERS_ENABLED \
+  -DHAVE_LIBUSB_1_0 \
+  -DLIBUSB_H_INCLUDE=\"mac/libusb/libusb.h\" \
+  -DMAXIMAL_ENABLED \
+  -DQT_CORE5COMPAT_LIB \
+  -DQT_CORE_LIB \
+  -DQT_NO_DEBUG \
+  -DSHAPELIB_ENABLED \
+  -I. \
+  -isystem /opt/homebrew/opt/qt/lib/QtCore.framework/Headers -iframework /opt/homebrew/opt/qt/lib -isystem /opt/homebrew/opt/qt/share/qt/mkspecs/macx-clang -isystem /opt/homebrew/opt/qt/include -isystem /opt/homebrew/opt/qt/lib/QtCore5Compat.framework/Headers -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk \
+  -Wall \
+  -Wsign-compare \
+  -std=gnu++17 \
+  -Winvalid-pch \
+  -Xarch_arm64 \
+  -include/Users/robertlipe/src/gpsbabel/CMakeFiles/gpsbabel.dir/cmake_pch_arm64.hxx \
+   -o short_test \
+   mkshort.cc util.cc fatal.cc \
+  -F/opt/homebrew/opt/qt/lib \
+  -Wl,-rpath,/opt/homebrew/opt/qt/lib /opt/homebrew/opt/qt/lib/QtCore5Compat.framework/Versions/A/QtCore5Compat libshp.a \
+  libz.a libusb-1.0.a \
+  /opt/homebrew/opt/qt/lib/QtCore.framework/Versions/A/QtCore \
+  -framework DiskArbitration -lobjc \
+  -framework IOKit \
+  -framework CoreFoundation \
+  -framework Security
+
+
+#   -MD -MT CMakeFiles/gpsbabel.dir/mkshort.cc.o \
+#   -MF CMakeFiles/gpsbabel.dir/mkshort.cc.o.d \
index 0c473e0acd4b416556634aa62abdf3449c125817..d96c248f395806c57959d69cde5e02069cec9c7c 100644 (file)
--- a/unicsv.cc
+++ b/unicsv.cc
@@ -410,14 +410,14 @@ UnicsvFormat::unicsv_fondle_header(QString header)
     const field_t* f = &fields_def[0];
 
     unicsv_fields_tab.append(fld_terminator);
-    while (f->name) {
+    while (!f->name.isEmpty()) {
       if (unicsv_compare_fields(value, f)) {
         unicsv_fields_tab.last() = f->type;
         break;
       }
       f++;
     }
-    if ((! f->name) && global_opts.debug_level) {
+    if ((f->name.isEmpty()) && global_opts.debug_level) {
       warning(MYNAME ": Unhandled column \"%s\".\n", qPrintable(value));
     }
 
@@ -488,8 +488,8 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
   double utm_easting = 0;
   double utm_northing = 0;
   char utm_zc = 'N';
-  // Zones are always two bytes.  Spare one for null termination..
-  char bng_zone[3] = "";
+  // Zones are always two bytes.
+  QString bng_zone;
   double bng_easting = kUnicsvUnknown;
   double bng_northing = kUnicsvUnknown;
   double swiss_easting = kUnicsvUnknown;
@@ -608,8 +608,7 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
       break;
 
     case fld_bng_zone:
-      strncpy(bng_zone, CSTR(value), sizeof(bng_zone) - 1);
-      strupper(bng_zone);
+      bng_zone = value.toUpper();
       break;
 
     case fld_bng_northing:
@@ -1028,7 +1027,7 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
       GPS_Math_UTM_EN_To_Known_Datum(&wpt->latitude, &wpt->longitude,
                                      utm_easting, utm_northing, utm_zone, utm_zc, unicsv_datum_idx);
     } else if ((bng_easting != kUnicsvUnknown) && (bng_northing != kUnicsvUnknown)) {
-      if (bng_zone[0] == '\0') { // OS easting northing
+      if (bng_zone.isEmpty()) { // OS easting northing
         // Grid references may also be quoted as a pair of numbers: eastings then northings in metres, measured from the southwest corner of the SV square.
         double bnge;
         double bngn;
@@ -1046,10 +1045,10 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
                 bngz, bnge, bngn);
       } else { // traditional zone easting northing
         if (! GPS_Math_UKOSMap_To_WGS84_M(
-              bng_zone, bng_easting, bng_northing,
+              CSTR(bng_zone), bng_easting, bng_northing,
               &wpt->latitude, &wpt->longitude))
           fatal(MYNAME ": Unable to convert BNG coordinates (%s %.f %.f)!\n",
-                bng_zone, bng_easting, bng_northing);
+                CSTR(bng_zone), bng_easting, bng_northing);
       }
       src_datum = kDautmWGS84; /* don't convert afterwards */
     } else if ((swiss_easting != kUnicsvUnknown) && (swiss_northing != kUnicsvUnknown)) {
index f2922ff19920493a8092ceecadb878cfac4e93a6..7d43094e4d2b5c0e18aa7857656f31364af8eae0 100644 (file)
--- a/unicsv.h
+++ b/unicsv.h
@@ -140,7 +140,7 @@ private:
   };
 
   struct field_t {
-    const char* name;
+    const QString name;
     field_e type;
     uint32_t options;
   };
diff --git a/util.cc b/util.cc
index a5bad51187539d9012f700ec9cc8905716128418..a650fa36de0ce7b23700321db368c5eb08b1e77f 100644 (file)
--- a/util.cc
+++ b/util.cc
@@ -318,26 +318,6 @@ xvasprintf(char** strp, const char* fmt, va_list ap)
   return outsize;
 }
 
-void
-rtrim(char* s)
-{
-  char* t = s;
-
-  if (!s || !*s) {
-    return;
-  }
-
-  while (*s) {
-    s++;
-  }
-
-  s--;
-  while ((s >= t) && isspace(*s)) {
-    *s = 0;
-    s--;
-  }
-}
-
 /*
  * compare str with match
  * match may contain wildcards "*" and "?"